﻿module Sustainalytics.News {

    export class NewsStatusService implements INewsStatusService {

        private items: NewsStatus[];
        private $q: ng.IQService;

        constructor($q: ng.IQService) {
            this.$q = $q;

            // create items
            this.items =
            [
                { id: 1, name: 'Published' },
                { id: 2, name: 'Unpublished' },
                { id: 3, name: 'Draft' }
            ];
        }

        // get news status by ID
        public getNewsStatus(id: number): ng.IPromise<NewsStatus> {
            var def: ng.IDeferred<NewsStatus> = this.$q.defer();

            var item: NewsStatus = null;
            $(this.items).each(function () {
                if (this.id == id) {
                    item = this;
                    return false;
                }
            });

            def.resolve(item);

            return def.promise;
        }

        // get news statuses
        public getNewsStatuses(): ng.IPromise<NewsStatus[]> {
            var def: ng.IDeferred<NewsStatus[]> = this.$q.defer();
            def.resolve(this.items);
            return def.promise;
        }
    }

    // register service with News Angular module
    angular.module('Sustainalytics.News').factory('NewsStatusService', function ($q) {
        var srv = new NewsStatusService($q);
        return srv;
    });
} 